package com.kancy.demo.kie.server.client.controller;

import com.kancy.demo.User;
import com.kancy.kie.server.client.service.DroolsRuleEngine;
import com.kancy.kie.server.client.service.RuleParams;
import com.kancy.kie.server.client.service.RuleParamsBuilder;
import com.kancy.kie.server.client.service.RuleResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 * TestController
 * <p>
 *
 * @author: kancy
 * @date: 2020/3/14 16:13
 **/
@Slf4j
@RequiredArgsConstructor
@RestController
public class TestController {

    private static String CONTAINER_ID = "com.kancy:demo3";
    private static String SESSION_NAME = "hello";

    private final DroolsRuleEngine droolsRuleEngine;

    @GetMapping("/test")
    public String test(){
        // 定义规则执行入参
        RuleParams ruleParam = RuleParamsBuilder.create(CONTAINER_ID,SESSION_NAME)
                // 插入一个对象到工作内存
                .insert("user", new User())
                .setGlobal("_map_", new HashMap<>())
                .build();
        // 调用规则引擎，并且得到执行结果
        RuleResults ruleResult = droolsRuleEngine.call(ruleParam);
        log.info("ruleResult : {}", ruleResult);
        if (ruleResult.isSuccess()){
            // 调用成功时，获取内存中的对象
            User user = ruleResult.getValue("user");
            log.info("user : {}", user);
        }
        return "ok";
    }
}
